﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.IO;
using System.Text;

public partial class Admin_UserControl_ucAddItem : System.Web.UI.UserControl
{

    TblItemsDAO ItemsDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblItemImageDAO imageDAO = new TblItemImageDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            //hypDetails.NavigateUrl = "~/Admin/WebContent.aspx?idItem=" + id;
            if (id == null || id == "")
            {
                fileUpload.Visible = false;
                lblImage.Visible = false;
               // a.Visible = false;
                //b.Visible = false;
                //c.Visible = false;
                lbltitle.Text = "Add New Product";
                LoadCategories();
            }
            else
            {
                lbltitle.Text = "Edit Product";
                LoadCategories();
                LoadItemById();
                LoadImgItemByID();
            }
        }
    }

    private void LoadImgItemByID()
    {
        string id = Request.QueryString["id"];
        grdImgItems.DataSource = ItemsDAO.GetImgItems(Convert.ToInt32(id));
        grdImgItems.DataBind();
    }

    private void LoadItemById()
    {
        LoadCategories();
        string id = Request.QueryString["id"];
        TblItems Item = ItemsDAO.GetItemByID(Convert.ToInt32(id));
        txtItemName.Text = Item.ItemName;
        txtNumber.Text = Item.ItemNumber;
        drCategory.SelectedValue = Item.CateID.ToString();
        txtDes.Content = Server.HtmlDecode(Item.ItemDescription);
        txtPrice.Text = Item.BaseSellingPrice.ToString();
        //drInactive.SelectedValue = Item.IsInactive;
    }

    private void LoadCategories()
    {
        drCategory.DataSource = ItemsDAO.GetCategory();
        drCategory.DataTextField = "CateName";
        drCategory.DataValueField = "CateID";
        drCategory.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool flag = false;
        string id = Request.QueryString["id"];
        try
        {
            TblItems Item = new TblItems();
            Item.ItemName = txtItemName.Text;
            Item.ItemNumber = txtNumber.Text;
            
            Item.IsInactive = "N";
            
            Item.QuantityOnHand = 0;
            Item.ValueOnHand = 0;
            Item.ItemDescription = Server.HtmlEncode(txtDes.Content);
            Item.CateID = Convert.ToInt32(drCategory.SelectedValue);
            Item.BaseSellingPrice = Convert.ToDouble(txtPrice.Text);
            Item.PriceIsInclusive = "N";
            Item.SellTaxCodeID = 2;

            if (id == null || id == "")
            {
                Item.ItemID = 0;
                ItemsDAO.InsUpdItem(Item, 1);
            }
            else
            {
                Item.ItemID = Convert.ToInt32(id);
                ItemsDAO.InsUpdItem(Item, 2);
            }
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/ListItems.aspx");
            else
                //Response.Redirect("~/Admin/AddItem.aspx?id=" + id);
                Response.Redirect("~/Admin/ListItems.aspx");
        }

    }
    protected void grdImgItems_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            foreach (DataControlFieldCell cell in e.Row.Cells)
            {
                // check all cells in one row
                foreach (Control control in cell.Controls)
                {
                    ImageButton button = control as ImageButton;
                    if (button != null && button.CommandName == "Delete")
                    {
                        // Add delete confirmation
                        button.CommandArgument = ((TblItemImage)e.Row.DataItem).ID.ToString();
                        button.OnClientClick = "if (!confirm('Are you sure to delete this Gallery?')) return false;";
                    }
                }
            }
        }
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblItemImage imgItem = new TblItemImage();

            imgItem.ID = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            imgItem = imageDAO.GetItemImageByID(Global.ConvertToInt(id), idXoa);   
            ItemsDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if(File.Exists(Server.MapPath("~\\Admin\\images\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\images\\Items\\" + filename));
            }
            
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            LoadImgItemByID();
        }

    }
    protected void btnDefault_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void btnReset_Click(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
           
            string path = Server.MapPath("~\\Admin\\images\\Items\\");
            string filename = id + "_" + fileUpload.FileName;

            TblItemImage imgItem = new TblItemImage();
           // lblMessage.Text = path;
            if (File.Exists(path + filename))
            {
                lblMessage.Text = "The image file already exist. Please choose other file or change the file name to upload";
                return;
            }
            fileUpload.SaveAs(path + filename);
            imgItem.ImageURL = filename;
            imgItem.ItemID = Convert.ToInt32(id);

            ItemsDAO.InsImageItem(imgItem, 1);
            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE_ADMIN);
        }
        if (flag)
        {
            if (id == null || id == "")
                Response.Redirect("~/Admin/AddItem.aspx?id=" + id);
            else
                Response.Redirect("~/Admin/AddItem.aspx?id=" + id);
        }
    }
}
